package com.linkoog.devtools.code_generator.wizard.generate.database.table.model.datasource;

import com.linkoog.devtools.code_generator.database.domain.ConnectionInfo;

import javax.swing.*;
import java.awt.*;

public class DatasourceComboBoxCellRender implements ListCellRenderer {

    private JLabel label;

    public DatasourceComboBoxCellRender() {
        label = new JLabel();
        label.setOpaque(true);
    }


    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        if (value == null) {
            label.setText("");
            return label;
        }

        if (value instanceof DatasourceOperationItem) {
            DatasourceOperationItem itemData = (DatasourceOperationItem)value;
            label.setIcon(itemData.getIcon());
            label.setText(itemData.getText());
        }else if (value instanceof ConnectionInfoItem){
            ConnectionInfoItem itemData = (ConnectionInfoItem) value;
            ConnectionInfo info = itemData.getConnectionInfo();

            StringBuilder builder = new StringBuilder(150);
            builder.append("<html>")
                    .append("<font color=\"#7F7F7F\">[").append(handlerName(info.getName())).append("]</font>")
                    .append("&nbsp;&nbsp;");

            if ( itemData.isHasConnection() == null){
                builder.append(info.getUsername()).append("@")
                        .append(info.getHost()).append(":").append(info.getPort()).append("/").append(info.getDatabase());
            } else if (itemData.isHasConnection()){
                builder.append("<font color=\"#444693\">");
                builder.append(info.getUsername()).append("@")
                        .append(info.getHost()).append(":").append(info.getPort()).append("/").append(info.getDatabase());
                builder.append("&nbsp;&nbsp;").append("[连接成功]");
                builder.append("</font>");
            } else {
                builder.append("<font color=\"#DF0101\">");
                builder.append(info.getUsername()).append("@")
                        .append(info.getHost()).append(":").append(info.getPort()).append("/").append(info.getDatabase());
                builder.append("&nbsp;&nbsp;").append("[连接失败]");
                builder.append("</font>");
            }


            builder.append("</html>");

            label.setIcon(info.getDbType().getIcon());
            label.setText(builder.toString());
        } else if (value instanceof String){
            label.setIcon(null);
            label.setText((String)value);
        }

        if (isSelected) {
            label.setBackground(list.getSelectionBackground());
            label.setForeground(list.getSelectionForeground());
        } else {
            label.setBackground(list.getBackground());
            label.setForeground(list.getForeground());
        }

        return label;
    }


    private String handlerName(String name) {
        int total = 15;
        if (name.length() >= total){
            name = name.substring(0, total - 4) + "...";
        }else {
            /*int padLen = total - methodName.length();
            StringBuilder temp = new StringBuilder(padLen);
            for (int i = 1; i <=  padLen; i++) {
                temp.append("&nbsp;");
            }
            methodName = methodName + temp.toString();*/
        }
        return name;
    }
}
